import { CaptchaService } from '@midwayjs/captcha';
import { Controller, Get, Inject, Post } from '@midwayjs/core';
import { JwtService } from '@midwayjs/jwt';

@Controller('/auth')
export class AuthController {
  @Inject()
  captchaService: CaptchaService;

  @Inject()
  jwtService: JwtService;

  @Get('/codes')
  async codes() {
    return {
      code: 0,
      data: ['AC_100100', 'AC_100110', 'AC_100120', 'AC_100010'],
      error: null,
      message: 'ok',
    };
  }

  @Get('/getCaptcha')
  async generate() {
    const { id, imageBase64 } = await this.captchaService.formula();
    return {
      code: 200,
      msg: 'success',
      data: {
        captchaId: id,
        img: imageBase64,
      },
    };
  }

  @Post('/login')
  async login() {
    // 4. 生成JWT
    return {
      code: 0,
      data: {
        id: 0,
        password: '123456',
        realName: 'Vben',
        roles: ['super'],
        username: 'vben',
        accessToken: await this.jwtService.sign({
          userId: '10000',
          username: 'admin',
        }),
      },
      error: null,
      message: 'ok',
    };
  }
}
